﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Xml.Linq;

namespace Lightray.Hattrick.ChppLib.EntityTypes
{
    [ComVisible(true)]
    public class Player : BasicPlayer
    {
        public Country Birthplace { get; protected set; }
        public bool IsForigner { get; protected set; }       
        public int TSI { get; private set; }
        public int Salary { get; private set; }

        public Agreeability Agreeability { get; private set; }
        public Aggressiveness Aggressiveness { get; private set; }
        public Honesty Honesty { get; private set; }

        public SkillLevel Form { get; private set; }
        public SkillLevel Experience { get; private set; }
        public SkillLevel Leadership { get; private set; }
        public SkillLevel Stamina { get; private set; }

        public int CupGoals { get; protected set; }

        public bool IsTransferListed { get; private set; }

        protected Player() { }

        public static EntityCollection<Player> FromXml(XDocument playersXml)
        {
            HattrickXmlReader reader = new HattrickXmlReader(playersXml);
            DateTime dataTime = reader.ReadDate("FetchedDate");
            int teamId = reader.ReadNumber("TeamID");

            List<Player> ret = new List<Player>();
            foreach (var player in playersXml.Descendants("Player"))
                ret.Add(Player.fromPlayerNode(player, teamId, dataTime));

            return ret.ToEntityCollection();
        }
        
        private static Player fromPlayerNode(XElement playerNode, int teamId, DateTime dataTime)
        {
            HattrickXmlReader reader = new HattrickXmlReader(playerNode);
            return new Player()
            {
                // TODO: change to consts
                ID = reader.ReadNumber("PlayerID"),
                DataTime = dataTime,
                Age = reader.ReadHattrickAge("Age", "AgeDays"),
                FirstName = reader.ReadString("FirstName"),
                LastName = reader.ReadString("LastName"),
                Birthplace  = reader.ReadCountry("CountryID"),
                TSI = reader.ReadNumber("TSI"),
                Form = reader.ReadSkill("PlayerForm"),
                Experience = reader.ReadSkill("Experience"),
                Leadership = reader.ReadSkill("Leadership"),                
                Salary = reader.ReadNumber("Salary"),
                IsForigner = reader.ReadBoolean("IsAbroad"),
                IsTransferListed = reader.ReadBoolean("TransferListed"),
                Cards = reader.ReadNumberOrDefault("Cards", -2),
                InjuryLevel = reader.ReadNumberOrDefault("InjuryLevel", -1),
                CareerGoals = reader.ReadNumberOrDefault("CareerGoals", 0),
                CareerHattricks = reader.ReadNumberOrDefault("CareerHattricks", 0),
                CupGoals = reader.ReadNumberOrDefault("CupGoals", 0),
                LeagueGoals = reader.ReadNumberOrDefault("LeagueGoals", 0),
                Agreeability = reader.ReadAgreeability("Agreeability"),
                Aggressiveness = reader.ReadAggressiveness("Aggressiveness"),
                Honesty = reader.ReadHonesty("Honesty"),
                Specialty = reader.ReadSpecialty("Specialty"),
                Keeper = reader.ReadSkill("KeeperSkill"),
                Defending = reader.ReadSkill("DefenderSkill"),
                Passing = reader.ReadSkill("PassingSkill"),
                Playmaking = reader.ReadSkill("PlaymakerSkill"),
                Scoring = reader.ReadSkill("ScorerSkill"),
                Winger = reader.ReadSkill("WingerSkill"),
                SetPieces = reader.ReadSkill("SetPiecesSkill"),
                Stamina  = reader.ReadSkill("StaminaSkill"),

                LastMatch = PlayerMatchStats.FromXml(reader.ReadElement("LastMatch")),

                TeamID = teamId,
            };
        }

        public override SkillLevel GetSkillLevel(Skill skill)
        {
            switch (skill)
            {
                case Skill.Form:
                    return Form;
                case Skill.Experience:
                    return Experience;
                case Skill.Leadership:
                    return Leadership;
                case Skill.Stamina:
                    return Stamina;                
                default:
                    return base.GetSkillLevel(skill);
            }            
        }
    }
}

  
  